package com.qfhd.soft.cjzcore.UI.Custom;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.qfhd.soft.cjzcore.R;

public class CJNaviBar extends RelativeLayout {

    private Context mContext;
    private AttributeSet mAttr;

    private ImageView   img_backIcon;
    private TextView    tx_title;
    private TextView    tx_right;
    private RelativeLayout background;
    private boolean     isHideRightBtn;

    public CJNaviBar(Context context) {
        this(context,null);
    }

    public CJNaviBar(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CJNaviBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }

    private void init(Context context,AttributeSet attr){
        mContext    = context;
        mAttr       = attr;

        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View mView              = inflater.inflate(R.layout.navibar_layout,this,true);
        img_backIcon            = mView.findViewById(R.id.navi_back);
        tx_title                = mView.findViewById(R.id.navi_title);
        tx_right                = mView.findViewById(R.id.navi_rightTextV);
        background              = mView.findViewById(R.id.navi_relativeLy);

        TypedArray typedArray = mContext.obtainStyledAttributes(attr, R.styleable.CJNaviBar);
        img_backIcon.setImageResource(typedArray.getResourceId(R.styleable.CJNaviBar_backIcon,R.drawable.back));
        tx_title.setText(typedArray.getString(R.styleable.CJNaviBar_title));
        tx_right.setText(typedArray.getString(R.styleable.CJNaviBar_rightBtnText));
        isHideRightBtn  = typedArray.getBoolean(R.styleable.CJNaviBar_isHideRightBtn,true);

        if(isHideRightBtn){
            LayoutParams params = (LayoutParams) tx_right.getLayoutParams();
            params.width = 0;
            tx_right.setLayoutParams(params);
        }
    }

    public void setLeftBtnClickListener(OnClickListener listener){
        if(listener != null)
            img_backIcon.setOnClickListener(listener);
    }

    public void setRightBtnClickListener(OnClickListener listener){
        if(listener != null)
            tx_right.setOnClickListener(listener);
    }

    public RelativeLayout getBackgroundLayout(){
        return background;
    }

    public void hideLeftBtn(){
        img_backIcon.setVisibility(INVISIBLE);
    }

    public TextView getTitle(){
        return tx_title;
    }
}
